﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.Objects;

namespace BookStore
{
    public partial class ChinhSuaTaiKhoan : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }
            MembershipUser obj = Membership.GetUser();//user chua dang nhap
            if (obj == null)
            {
                return;
            }

            using (var context = new BookStoreEntities())
            {


                Guid uId = (Guid)obj.ProviderUserKey;//user id

                var sQuery = "select value kh from BookStoreEntities.KhachHangs as kh"
                + " where kh.UserID=@UserID";

                ObjectQuery<KhachHang> khs = context.CreateQuery<KhachHang>(sQuery);
                khs.Parameters.Add(new ObjectParameter("UserID", uId));
                //KhachHang k = new KhachHang();
                List<KhachHang> kh = khs.ToList<KhachHang>();
                if (kh.Count > 0)
                {
                    String s = kh[0].DiaChi;
                    txtFullName.Text = kh[0].TenKhachHang;
                    txtAddress.Text = kh[0].DiaChi;
                    txtPhoneNumber.Text = kh[0].DienThoai;
                    txtEmail.Text = kh[0].Email;
                }

            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {

            using (BookStoreEntities context = new BookStoreEntities())
            {
                string fullName = txtFullName.Text;
                string email = txtEmail.Text;
                string phoneNumber = txtPhoneNumber.Text;
                string address = txtAddress.Text;


                MembershipUser obj = Membership.GetUser();
                Guid id = (Guid)obj.ProviderUserKey;

                KhachHang kh = (from k in context.KhachHangs
                                where k.UserID == id
                                select k).First();
                kh.TenKhachHang = fullName;
                kh.Email = email;
                kh.DiaChi = address;
                kh.DienThoai = phoneNumber;
                context.SaveChanges();
                lbRes.Text = "Updating successfully";
                Page_Load(sender, e);
            }
        }
    }
}